Page({
  data: {},
  onLoad() {
    let pay = getApp().payData;
    let that = this;
    my.getAuthCode({
      scopes: 'auth_base',
      success: (res) => {
        my.request({
          url: 'https://openpay.duolaibei.com/mobile/getOpenID', 
          method: 'POST',
          data: {
            type: 2,
            code: res.authCode
          },
          success: (result) => {
            if (result.data.code == 200) {
              my.request({
                url: 'https://openpay.duolaibei.com/mobile/alipayweb',
                method: 'POST',
                data: {
                  merchantID: pay.merchantID,
                  openID: result.data.content,
                  money: pay.money,
                  type: '3',
                  body: pay.body ? pay.body : pay.merchantName,
                  note: pay.note ? pay.note : '',
                  notify_url: pay.notify_url,
                  external_order_no: pay.external_order_no,
                },
                success: (res) => {
                  if (res.data.code == 200) {
                    that.pay(res.data.content.payData.tradeNO,res.data.content.orderNo)
                  } else {
                    my.confirm({
                      content: '下单失败！请联系服务商',
                      confirmButtonText: '确认',
                      success: (result) => {
                        my.navigateTo({
                          // url: '/pages/webview/webview?status=4&order_no=' + res.data.content.orderNo
                          url: '/pages/custom/custom?status=4'
                        });
                      }
                    });
                  }
                }
              });
            }
          }
        });
      }
    });
  },
  pay(tradeNO,orderNo) {
    my.tradePay({
      tradeNO: tradeNO,
      success: (result) => {
        if (result.resultCode == 9000) { //成功
          my.navigateTo({
          // url: '/pages/webview/webview?status=1&order_no=' + orderNo
            url: '/pages/custom/custom?status=1'
          });
        } else if (result.resultCode == 6001) { //取消
          my.confirm({
            content: '支付取消',
            confirmButtonText: '确认',
            success: (result) => {
              my.navigateTo({
                //url: '/pages/webview/webview?status=2&order_no=' + orderNo
                url: '/pages/custom/custom?status=2'
              });
            }
          });
        } else if (result.resultCode == 4000) {//失败
          my.confirm({
            content: '支付失败',
            confirmButtonText: '确认',
            success: (result) => {
              my.navigateTo({
                // url: '/pages/webview/webview?status=3&order_no=' + orderNo
                url: '/pages/custom/custom?status=3'
              });
            }
          });

        } else {
          my.confirm({
            content: '下单失败',
            confirmButtonText: '确认',
            success: (result) => {
              my.navigateTo({
                // url: '/pages/webview/webview?status=4&order_no=' + orderNo
                url: '/pages/custom/custom?status=4'
              });
            }
          });
        }
      },
      fail: (err) => {
        my.confirm({
          content: '支付异常！',
          confirmButtonText: '确认',
          success: (result) => {
            my.navigateTo({
              // url: '/pages/webview/webview?status=3&order_no=' + orderNo
              url: '/pages/custom/custom?status=3'
            });
          }
        });
      }
    });
  }
});
